
import os

from selenium.webdriver.support.wait import WebDriverWait
import time

from base import log
from config import DIR_PATH


class Base:
    def __init__(self, driver):
        log.info(f"正在初始化driver对象:{driver}")
        self.driver = driver

    # 查找元素
    def base_find_el(self, loc, timeout=10, poll_frequency=0.5):
        log.info(f"正在查找元素:{loc}")
        return WebDriverWait(self.driver, timeout, poll_frequency).until(
            lambda x: x.find_element(*loc))

    # 点击元素
    def base_click_el(self, loc):
        log.info(f"正在点击元素:{loc}")
        # 定位元素
        el = self.base_find_el(loc)
        # 点击元素
        el.click()

    # 输入内容
    def base_input_el(self, loc, value):
        log.info(f"正在输入元素:{loc}, 输入内容为:{value}")
        el = self.base_find_el(loc)
        el.clear()
        el.send_keys(value)

    # 获取文本
    def base_get_text(self, loc):
        log.info(f"正在调用获取元素信息方法:{loc}")
        el = self.base_find_el(loc)
        return el.text

    # 截图
    def base_screenshot(self):
        log.info(f"正在截图")
        # 图片路径
        img_path = DIR_PATH + os.sep + "img" + os.sep + f"error_{time.strftime('%Y_%m_%d_%H_%M_%S')}.png"
        self.driver.get_screenshot_as_file(img_path)

    # 切换到iframe框架
    def base_switch_frame(self, loc):
        log.info(f"正在切换frame框架:{loc}")
        el = self.base_find_el(loc)
        self.driver.switch_to.frame(el)

    # 恢复到默认框架
    def base_default_frame(self):
        self.driver.switch_to.default_content()
